package drds.data_migrate.util;


import drds.common.Author;
import drds.data_migrate.Authors;
import lombok.extern.slf4j.Slf4j;

import java.sql.*;
import java.util.Properties;
//com.mysql.jdbc.Driver
//yearIsDateType=false&zeroDateTimeBehavior=convertToNull&tinyInt1isBit=false&rewriteBatchedStatements=true

@Author(name = Authors.LI_YANG)
@Slf4j
public class Jdbc
{
    public static final int SOCKET_TIMEOUT_INSECOND = 172800;

    public static void close(ResultSet resultSet, Statement statement, Connection connection)
    {
        if (null != resultSet)
        {
            try
            {
                resultSet.close();
            } catch (SQLException e)
            {
                e.printStackTrace();
                log.error(e.getMessage());
            }
        }

        if (null != statement)
        {
            try
            {
                statement.close();
            } catch (SQLException e)
            {
                e.printStackTrace();
                log.error(e.getMessage());
            }
        }

        if (null != connection)
        {
            try
            {
                connection.close();
            } catch (SQLException e)
            {
                e.printStackTrace();
                log.error(e.getMessage());
            }
        }
    }

    public static void close(Statement statement, Connection connection)
    {
        close(null, statement, connection);
    }

    public static void close(Connection connection)
    {
        close(null, null, connection);
    }

    public static void close(Statement statement)
    {
        close(null, statement, null);
    }

    //

    public static void close(ResultSet resultSet)
    {
        close(resultSet, null, null);
    }

    public static void execute(Statement statement, String sql)
            throws SQLException
    {
        statement.execute(sql);
    }

    public static ResultSet executeQuery(Statement statement, String sql)
            throws SQLException
    {
        return statement.executeQuery(sql);
    }

    public static ResultSet executeQuery(Connection connection, String sql)
            throws SQLException
    {
        // 默认3600 s 的query Timeout
        return executeQuery(connection, sql, SOCKET_TIMEOUT_INSECOND);
    }

    public static ResultSet executeQuery(Connection connection, String sql, int queryTimeout)
            throws SQLException
    {

        connection.setAutoCommit(false);
        Statement statement = connection.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
        statement.setQueryTimeout(queryTimeout);
        return executeQuery(statement, sql);
    }

    static synchronized Connection getConnection(String url, Properties properties) throws SQLException
    {
        try
        {
            return DriverManager.getConnection(url, properties);
        } catch (SQLException e)
        {
            log.error(e.getMessage());
            throw e;
        }
    }

    //
    public static synchronized Connection getConnection(String url, String username, String password) throws SQLException
    {
        Properties properties = new Properties();
        properties.put("user", username);
        properties.put("password", password);
        return getConnection(url, properties);
    }

    public static class JdbcCreateConnectionFactory
    {


        private String jdbcUrl;

        private String userName;

        private String password;

        public JdbcCreateConnectionFactory(String jdbcUrl, String userName, String password)
        {

            this.jdbcUrl = jdbcUrl;
            this.userName = userName;
            this.password = password;
        }


        public Connection getConnection() throws SQLException
        {
            return Jdbc.getConnection(jdbcUrl, userName, password);
        }


    }


}
